<?php

namespace App\Http\Controllers\Home;

use App\Models\Member;
use App\Models\MemberWaterBill;
use App\Models\SaoPay;
use App\Models\Shop;
use App\Models\WaterBill;
use App\Models\WaterBillOrder;
use App\Models\WechatSet;


/**
 * 订单控制器
 */
class WaterBillController extends BaseController
{

    public function __construct(
        MemberWaterBill $memberWaterBill,
        WechatSet $wechatSet,
        WaterBill $waterBill,
        Shop $shop,
        WaterBillOrder $waterBillOrder,
        Member $member,
        SaoPay $saoPay
    )
    {
        parent::__construct();
        $this->memberWaterBill = $memberWaterBill;
        $this->waterBill = $waterBill;
        $this->wechatSet = $wechatSet;
        $this->shop = $shop;
        $this->waterBillOrder = $waterBillOrder;
        $this->member = $member;
        $this->saoPay = $saoPay;
    }

    public function checkShopStatus()
    {
        $shop = $this->shop->_redisGetOneBySid($this->shop_id);
        $msg = '';
        if ($shop['status'] != 1) {
            $msg = '系统维护升级中，暂时无法下单';
        }
        return $msg;
    }

    public function postAdd()
    {
        if ($msg = $this->checkShopStatus()) {
            return error($msg);
        }
        $memberInfo = $this->member->_redisGetOneByXcxOpenId($this->xcx_openid, $this->shop_id);
        if (!$memberInfo) {
            return error('非法操作');
        }
        if (!$memberInfo['status']) {
            return error('您已被禁用，无法购买，请联系商家');
        }
        //存储用户formId
        $this->member->_redisSetFormId($this->mid, request('formId'));
        $res = $this->waterBillOrder->addOrder();
        if (!$res) {
            return error($this->waterBillOrder->getError());
        }
        return success();
    }

    /**
     * 预支付请求接口(POST)
     * $openid         openid
     * $body      商品简单描述
     * $order_sn    订单编号
     * $total_fee   金额
     * json的数据
     */
    public function postPay()
    {
        if ($msg = $this->checkShopStatus()) {
            return error($msg);
        }
        $memberInfo = $this->member->getMemberInfoByOpenId($this->xcx_openid, $this->wx_openid, $this->shop_id);
        if (!$memberInfo) {
            return error('非法操作');
        }
        if (!$memberInfo['status']) {
            return error('您已被禁用，无法购买，请联系商家');
        }
        $order = $this->waterBillOrder->addOrder();
        if (!$order || empty($order)) {
            return error($this->waterBillOrder->getError());
        }
        if ($order['type']) {
            return success();
        }
        $openid = request('openid');
        $result = $this->saoPay->prepay($this->xcx_openid, $this->wx_openid, $order, $openid, 2);
        if (!$result) {
            return error($this->saoPay->getError() ? $this->saoPay->getError() : '未知错误');
        }
        $result['id'] = $order['id'];
        return success($result);
    }

    /**
     * 支付回调
     * 返回数组去修改支付订单数据
     */
    public function anyPayNotify()
    {
        $this->saoPay->notify(2);
    }

    public function getCheckStatus()
    {
        $orderId = request('id');
        if (!$orderId) {
            return error('非法操作');
        }
        $res = $this->waterBillOrder->_getOne(['id' => $orderId]);
        if (!$res) {
            return error('该订单不存在');
        }
        $ajax['result'] = $res['pay_status'] == 20 ? true : false;
        return success($ajax);
    }


}
